<template>
  <div class="chat-container">
    <!-- 聊天头部 -->
    <div class="chat-header">
      <div class="user-info">
        <span class="username">{{ username }}</span>
        <span class="user-status">在线</span>
      </div>
      <el-dropdown trigger="click" class="chat-settings">
        <i class="el-icon-more"></i>
        <el-dropdown-menu slot="dropdown" class="settings-menu">
          <el-dropdown-item class="setting-item">
            <span>置顶该聊天</span>
            <el-switch v-model="setting.value1" active-color="#e4393c" inactive-color="#dcdfe6">
            </el-switch>
          </el-dropdown-item>
          <el-dropdown-item class="setting-item">
            <span>折叠该消息</span>
            <el-switch v-model="setting.value2" active-color="#e4393c" inactive-color="#dcdfe6">
            </el-switch>
          </el-dropdown-item>
          <el-dropdown-item class="setting-item">
            <span>加入黑名单</span>
            <el-switch v-model="setting.value3" active-color="#e4393c" inactive-color="#dcdfe6">
            </el-switch>
          </el-dropdown-item>
          <el-dropdown-item class="setting-item">
            <span>消息免打扰</span>
            <el-switch v-model="setting.value4" active-color="#e4393c" inactive-color="#dcdfe6">
            </el-switch>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <!-- 聊天消息区域 -->
    <div class="chat-messages" ref="chatMessages">
      <div class="message-container" v-for="(message, index) in messages" :key="message.id || index">
        <div class="message" :class="{ 'message-right': isMessageFromMe(message) }">
          <div class="message-avatar" v-if="isMessageFromMe(message)">
            <el-avatar :size="40" :src="message.senderAvatarUrl"></el-avatar>
          </div>
          <div class="message-content">
            <div class="message-time">{{ message.createdAt }}</div>
            <div class="message-bubble" :class="{ 'bubble-right': isMessageFromMe(message) }">
              {{ message.content }}
            </div>
          </div>
          <div class="message-avatar" v-if="!isMessageFromMe(message)">
            <el-avatar :size="40" :src="message.senderAvatarUrl"></el-avatar>
          </div>
        </div>
      </div>
    </div>

    <!-- 聊天输入区域 -->
    <div class="chat-input-container">
      <div class="input-toolbar">
        <div class="toolbar-left">
          <i class="el-icon-picture" title="发送图片"></i>
          <i class="el-icon-folder" title="发送文件"></i>
          <i class="el-icon-goods" title="选择商品"></i>
        </div>
      </div>
      <div class="input-area">
        <el-input
          v-model="inputText"
          type="textarea"
          :rows="3"
          placeholder="请输入消息..."
          resize="none"
          @keyup.enter.native="sendMessage"
        ></el-input>
      </div>
      <div class="input-footer">
        <span class="input-tip">Enter 发送消息</span>
        <el-button type="danger" @click="sendMessage" :disabled="!inputText.trim()">
          发送
        </el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #ffffff;
  margin-left: 15px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 聊天头部样式 */
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  height: 60px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.username {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.user-status {
  font-size: 12px;
  color: #67c23a;
}

.chat-settings {
  cursor: pointer;
}

.chat-settings i {
  font-size: 20px;
  color: #666;
}

.settings-menu {
  min-width: 200px;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
}

/* 聊天消息区域样式 */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background-color: #ffffff;
}

.message-container {
  margin-bottom: 20px;
}

.message {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.message-right {
  flex-direction: row-reverse;
}

.message-avatar {
  flex-shrink: 0;
}

.message-content {
  display: flex;
  flex-direction: column;
  max-width: 60%;
}

.message-time {
  font-size: 12px;
  color: #999;
  margin-bottom: 4px;
  text-align: left;
}

.message-right .message-time {
  text-align: right;
}

.message-bubble {
  background-color: #f0f0f0;
  padding: 12px 16px;
  border-radius: 16px 4px 16px 16px;
  font-size: 14px;
  color: #333;
  line-height: 1.5;
  position: relative;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.bubble-right {
  background-color: #1989fa;
  color: #fff;
  border-radius: 4px 16px 16px 16px;
}

/* 聊天输入区域样式 */
.chat-input-container {
  background: #fff;
  border-top: 1px solid #f0f0f0;
  padding: 16px;
}

.input-toolbar {
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.toolbar-left {
  display: flex;
  gap: 16px;
}

.toolbar-left i {
  font-size: 20px;
  color: #666;
  cursor: pointer;
  transition: color 0.3s ease;
}

.toolbar-left i:hover {
  color: #e4393c;
}

.input-area {
  padding: 12px 0;
}

.input-area .el-textarea__inner {
  border: none;
  padding: 8px 0;
  font-size: 14px;
  resize: none;
}

.input-area .el-textarea__inner:focus {
  box-shadow: none;
}

.input-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 8px;
}

.input-tip {
  font-size: 12px;
  color: #999;
}

.el-button--danger {
  background-color: #e4393c;
  border-color: #e4393c;
  padding: 8px 24px;
}

.el-button--danger:hover {
  background-color: #f15b5f;
  border-color: #f15b5f;
}

.el-button--danger[disabled] {
  background-color: #fab6b6;
  border-color: #fab6b6;
}

/* 滚动条样式 */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background-color: #ddd;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-track {
  background-color: transparent;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-container {
    margin: 0;
    border-radius: 0;
  }

  .message-content {
    max-width: 80%;
  }
}
</style>

<script>
export default {
  props: ['id'],
  data() {
    return {
      messages: [],
      inputText: '',
      userid: '',
      username: "wang",
      setting: {
        value1: false,
        value2: false,
        value3: false,
        value4: false
      }
    }
  },
  mounted() {
    this.scrollToBottom();
  },
  created() {
    this.getrolt();
  },
  methods: {
    showMoreOptions() {
      console.log('More options clicked');
    },
    load() {
      this.request.get(`/privatemessages/${this.id}`).then(res => {
        this.messages = res.data;
      });
    },
    getrolt() {
      this.request.get("/users/info").then(res => {
        if (res && res.data.userId) {
          this.userid = res.data.userId;
          this.load();
        }
      }).catch(error => {
        console.error("Error fetching user info:", error);
      });
    },
    isMessageFromMe(message) {
      return parseInt(message.receiverId) !== parseInt(this.userid);
    },
    scrollToBottom() {
      this.$nextTick(() => {
        const chatMessages = this.$refs.chatMessages;
        if (chatMessages) {
          chatMessages.scrollTop = chatMessages.scrollHeight;
        }
      });
    },
    sendMessage() {
      if (!this.inputText.trim()) return;

      this.request.post("/privatemessages/create", {
        content: this.inputText,
        receiverId: this.id
      }).then(res => {
        this.load();
        this.$notify({
          title: res.data,
          type: 'success'
        });
        this.inputText = '';
        this.scrollToBottom();
      }).catch(error => {
        console.error("Error sending message:", error);
        this.$message.error('发送消息失败，请重试');
      });
    }
  }
}
</script>